import axios, {AxiosError} from "axios";

export const RedirectResponseInterceptor = (resp: any) => {
  if (!resp.request.responseURL.includes(resp.config.url) && !resp.request.responseURL.includes(encodeURI(resp.config.url))) {
    window.location.href = resp.request.responseURL;
    return Promise.reject(resp);
  } else if (resp instanceof AxiosError) {
    return Promise.reject(resp);
  } else {
    return resp;
  }
};

export const enableRedirectResponseInterceptor = () => {
  axios.interceptors.response.use(RedirectResponseInterceptor, RedirectResponseInterceptor);
}
